<?php

namespace app\api\model\music;

use app\logic\model\music\MusicLogicModel;
use app\logic\model\user\UserLogicModel;
use app\logic\single\GoodsSingle;

class MusicApiModel extends MusicLogicModel
{
    public function getSelect(array $where=[],string $field='*',string $order='id desc',int $limit=15){

        return $this->where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
    }

    public function getToday(array $where=[],string $field='*',string $order='id desc',int $limit=15)
    {
        return $this->where($where)
            ->whereTime('create_time','today')
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
    }

    public function getApiList(array $param,$where=[],$filed="*",$page=1,$limit=15,$order="id desc"){
        $query = $this->where($where)
            ->field($filed)
            ->where(function($q) use ($param){
                if(check_request($param,'single_status')){
                    $q->where('single_status',1);
                }
                if(check_request($param,'writer_id')){
                    $q->where('user_id',$param['writer_id']);
                }
                if(check_request($param,'title')){
                    $q->whereLike('music_api_model.title',"%{$param['title']}%");
                }
            })
            ->withJoin([
                'writer'=>$this->writer_field,
                'type'=>['title']
            ]);
        $count = $query->count();
        $list = $query->limit($limit)
            ->select();
        return [$list,$count];
    }


    public function details($id, string $field='*')
    {
        $info = $this->getFindById($id,$field);
        if(empty($info)){
            return false;
        }
        $info['writer'] = UserLogicModel::getInfoById($info['user_id'],$this->writer_field);
        $info['is_collection'] = GoodsSingle::getInstance()->isCollection($id,10);
        $info['type'] = MusicTypeApiModel::where('id',$info['type_id'])->field('title')->find();
        return $info;
    }

}